package spem.process.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

import spem.Activity;
import spem.ControlFlow;
import spem.Decision;
import spem.Element;
import spem.End;
import spem.Iteration;
import spem.Phase;
import spem.Process;
import spem.SpemPackage;
import spem.Start;
import spem.process.diagram.edit.parts.Activity2EditPart;
import spem.process.diagram.edit.parts.ActivityEditPart;
import spem.process.diagram.edit.parts.ControlFlowEditPart;
import spem.process.diagram.edit.parts.DecisionEditPart;
import spem.process.diagram.edit.parts.EndEditPart;
import spem.process.diagram.edit.parts.IterationEditPart;
import spem.process.diagram.edit.parts.PhaseEditPart;
import spem.process.diagram.edit.parts.ProcessEditPart;
import spem.process.diagram.edit.parts.StartEditPart;
import spem.process.diagram.providers.SpemElementTypes;

/**
 * @generated
 */
public class SpemDiagramUpdater {

	/**
	 * @generated
	 */
	public static List<SpemNodeDescriptor> getSemanticChildren(View view) {
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case ProcessEditPart.VISUAL_ID:
			return getProcess_1000SemanticChildren(view);
		case IterationEditPart.VISUAL_ID:
			return getIteration_2002SemanticChildren(view);
		case PhaseEditPart.VISUAL_ID:
			return getPhase_2001SemanticChildren(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemNodeDescriptor> getProcess_1000SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		Process modelElement = (Process) view.getElement();
		LinkedList<SpemNodeDescriptor> result = new LinkedList<SpemNodeDescriptor>();
		for (Iterator<?> it = modelElement.getNestedElements().iterator(); it
				.hasNext();) {
			Element childElement = (Element) it.next();
			int visualID = SpemVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == EndEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == StartEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == ActivityEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == DecisionEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == IterationEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == PhaseEditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemNodeDescriptor> getIteration_2002SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		Iteration modelElement = (Iteration) view.getElement();
		LinkedList<SpemNodeDescriptor> result = new LinkedList<SpemNodeDescriptor>();
		for (Iterator<?> it = modelElement.getNestedElements().iterator(); it
				.hasNext();) {
			Element childElement = (Element) it.next();
			int visualID = SpemVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == Activity2EditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemNodeDescriptor> getPhase_2001SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		Phase modelElement = (Phase) view.getElement();
		LinkedList<SpemNodeDescriptor> result = new LinkedList<SpemNodeDescriptor>();
		for (Iterator<?> it = modelElement.getNestedElements().iterator(); it
				.hasNext();) {
			Element childElement = (Element) it.next();
			int visualID = SpemVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == Activity2EditPart.VISUAL_ID) {
				result.add(new SpemNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getContainedLinks(View view) {
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case ProcessEditPart.VISUAL_ID:
			return getProcess_1000ContainedLinks(view);
		case EndEditPart.VISUAL_ID:
			return getEnd_2004ContainedLinks(view);
		case StartEditPart.VISUAL_ID:
			return getStart_2005ContainedLinks(view);
		case ActivityEditPart.VISUAL_ID:
			return getActivity_2003ContainedLinks(view);
		case DecisionEditPart.VISUAL_ID:
			return getDecision_2006ContainedLinks(view);
		case IterationEditPart.VISUAL_ID:
			return getIteration_2002ContainedLinks(view);
		case PhaseEditPart.VISUAL_ID:
			return getPhase_2001ContainedLinks(view);
		case Activity2EditPart.VISUAL_ID:
			return getActivity_3002ContainedLinks(view);
		case ControlFlowEditPart.VISUAL_ID:
			return getControlFlow_4001ContainedLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getIncomingLinks(View view) {
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case EndEditPart.VISUAL_ID:
			return getEnd_2004IncomingLinks(view);
		case StartEditPart.VISUAL_ID:
			return getStart_2005IncomingLinks(view);
		case ActivityEditPart.VISUAL_ID:
			return getActivity_2003IncomingLinks(view);
		case DecisionEditPart.VISUAL_ID:
			return getDecision_2006IncomingLinks(view);
		case IterationEditPart.VISUAL_ID:
			return getIteration_2002IncomingLinks(view);
		case PhaseEditPart.VISUAL_ID:
			return getPhase_2001IncomingLinks(view);
		case Activity2EditPart.VISUAL_ID:
			return getActivity_3002IncomingLinks(view);
		case ControlFlowEditPart.VISUAL_ID:
			return getControlFlow_4001IncomingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getOutgoingLinks(View view) {
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case EndEditPart.VISUAL_ID:
			return getEnd_2004OutgoingLinks(view);
		case StartEditPart.VISUAL_ID:
			return getStart_2005OutgoingLinks(view);
		case ActivityEditPart.VISUAL_ID:
			return getActivity_2003OutgoingLinks(view);
		case DecisionEditPart.VISUAL_ID:
			return getDecision_2006OutgoingLinks(view);
		case IterationEditPart.VISUAL_ID:
			return getIteration_2002OutgoingLinks(view);
		case PhaseEditPart.VISUAL_ID:
			return getPhase_2001OutgoingLinks(view);
		case Activity2EditPart.VISUAL_ID:
			return getActivity_3002OutgoingLinks(view);
		case ControlFlowEditPart.VISUAL_ID:
			return getControlFlow_4001OutgoingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getProcess_1000ContainedLinks(
			View view) {
		Process modelElement = (Process) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getEnd_2004ContainedLinks(View view) {
		End modelElement = (End) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getStart_2005ContainedLinks(View view) {
		Start modelElement = (Start) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_2003ContainedLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getDecision_2006ContainedLinks(
			View view) {
		Decision modelElement = (Decision) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getIteration_2002ContainedLinks(
			View view) {
		Iteration modelElement = (Iteration) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getPhase_2001ContainedLinks(View view) {
		Phase modelElement = (Phase) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_3002ContainedLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getControlFlow_4001ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getEnd_2004IncomingLinks(View view) {
		End modelElement = (End) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getStart_2005IncomingLinks(View view) {
		Start modelElement = (Start) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_2003IncomingLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getDecision_2006IncomingLinks(
			View view) {
		Decision modelElement = (Decision) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getIteration_2002IncomingLinks(
			View view) {
		Iteration modelElement = (Iteration) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getPhase_2001IncomingLinks(View view) {
		Phase modelElement = (Phase) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_3002IncomingLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_ControlFlow_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getControlFlow_4001IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getEnd_2004OutgoingLinks(View view) {
		End modelElement = (End) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getStart_2005OutgoingLinks(View view) {
		Start modelElement = (Start) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_2003OutgoingLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getDecision_2006OutgoingLinks(
			View view) {
		Decision modelElement = (Decision) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getIteration_2002OutgoingLinks(
			View view) {
		Iteration modelElement = (Iteration) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getPhase_2001OutgoingLinks(View view) {
		Phase modelElement = (Phase) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getActivity_3002OutgoingLinks(
			View view) {
		Activity modelElement = (Activity) view.getElement();
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_ControlFlow_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<SpemLinkDescriptor> getControlFlow_4001OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	private static Collection<SpemLinkDescriptor> getContainedTypeModelFacetLinks_ControlFlow_4001(
			Element container) {
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		for (Iterator<?> links = container.getFlows().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof ControlFlow) {
				continue;
			}
			ControlFlow link = (ControlFlow) linkObject;
			if (ControlFlowEditPart.VISUAL_ID != SpemVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Element dst = link.getSource();
			result.add(new SpemLinkDescriptor(container, dst, link,
					SpemElementTypes.ControlFlow_4001,
					ControlFlowEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<SpemLinkDescriptor> getIncomingTypeModelFacetLinks_ControlFlow_4001(
			Element target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<SpemLinkDescriptor> result = new LinkedList<SpemLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != SpemPackage.eINSTANCE
					.getControlFlow_Source()
					|| false == setting.getEObject() instanceof ControlFlow) {
				continue;
			}
			ControlFlow link = (ControlFlow) setting.getEObject();
			if (ControlFlowEditPart.VISUAL_ID != SpemVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			if (false == link.eContainer() instanceof Element) {
				continue;
			}
			Element container = (Element) link.eContainer();
			result.add(new SpemLinkDescriptor(container, target, link,
					SpemElementTypes.ControlFlow_4001,
					ControlFlowEditPart.VISUAL_ID));

		}
		return result;
	}

}
